def setup
  size 200, 200
  background 0
  noFill
  smooth
  ellipseMode CENTER_RADIUS
  
  cols = 4
  radius = width / cols / 2
  grid cols, cols, radius * 2, radius * 2 do |x, y|
    x += radius
    y += radius
    create_gradient x, y, radius, rand_color, rand_color
  end
end

def rand_color
  color rand_bright, rand_bright, rand_bright
end

def rand_bright
  random(255).to_i
end

def create_gradient(x, y, radius, c1, c2)
  r, g, b = red(c1), green(c1), blue(c1)
  delta_r = red(c2) - r
  delta_g = green(c2) - g
  delta_b = blue(c2) - b
  
  gap_filler = 1 / 3.0
  0.step radius - 1, gap_filler do |n|
    coef = n / radius.to_f
    c = color r + delta_r * coef,
              g + delta_g * coef,
              b + delta_b * coef
    stroke c
    ellipse x, y, n, n
  end
  
  # adds smooth edge 
  # hack anti-aliasing
  stroke 0
  strokeWeight 3
  ellipse x, y, radius, radius
end
